/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductManagement.java
 *
 * Created on Mar 27, 2011, 11:16:43 PM
 */
package vn.aptech.project.view;

import vn.aptech.project.control.category_BLL;
import vn.aptech.project.control.orderline_BLL;
import vn.aptech.project.control.product_BLL;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr.Hiep
 */
public class ProductManagement extends javax.swing.JPanel {

    /** Creates new form ProductManagement */
    public ProductManagement() throws IOException {
        initComponents();
        try {
            category_BLL cgb = new category_BLL();
            Vector vt = cgb.selectTypeinfo();
            for (int i = 0; i < vt.size(); i++) {
                String abc = vt.get(i).toString();
                String temp = abc.substring(1, abc.length());
                String temm = temp.substring(0, temp.length() - 1);
                CbbCategory.addItem(temm);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
        SelectProduct();
    }

    public void SelectProduct() throws IOException {
        try {
            product_BLL pb = new product_BLL();
            Vector vt;
            if (txtProductName.getText().equals("")) {
                txtProductName.setText("Enter product name");
            }
            if (txtProductid.getText().equals("")) {
                txtProductid.setText("Enter product id");
            }
            if (!txtProductName.getText().equals("Enter product name") && !txtProductid.getText().equals("Enter product id")) {
                vt = pb.SearchByIdAndByName(Integer.parseInt(txtProductid.getText()), txtProductName.getText());
                CbbCategory.setSelectedItem("--Selected--");
            } else if (!txtProductName.getText().equals("Enter product name")) {
                vt = pb.SearchProductByName(txtProductName.getText());
                CbbCategory.setSelectedItem("--Selected--");
            } else if (!txtProductid.getText().equals("Enter product id")) {
                vt = pb.SearchProductById(Integer.parseInt(txtProductid.getText()));
                CbbCategory.setSelectedItem("--Selected--");
            } else {
                vt = pb.selectproduct();
                CbbCategory.setSelectedItem("--Selected--");
            }

            Vector vtCol = new Vector();
            vtCol.add("Id");
            vtCol.add("Category");
            vtCol.add("Product name");
            vtCol.add("Unit price");
            vtCol.add("Quantity");
            DefaultTableModel dtm = new DefaultTableModel(vt, vtCol) {

                @Override
                public boolean isCellEditable(int rowIndex, int mColIndex) {
                    return false;
                }
            };

            tblProduct.setModel(dtm);
        } catch (SQLException ex) {
            Logger.getLogger(ProductManagement.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        txtProductName = new javax.swing.JTextField();
        txtProductid = new javax.swing.JTextField();
        bntSearchPro = new javax.swing.JButton();
        CbbCategory = new javax.swing.JComboBox();
        bntCreateProduct = new javax.swing.JButton();
        bntCategory = new javax.swing.JButton();
        bntDeleteProduct = new javax.swing.JButton();
        bntEditProduct = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tblProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblProduct);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 540, 357));

        txtProductName.setText("Enter product name");
        add(txtProductName, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 370, 190, -1));

        txtProductid.setText("Enter product id");
        txtProductid.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtProductidKeyTyped(evt);
            }
        });
        add(txtProductid, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 370, 93, -1));

        bntSearchPro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Search.png"))); // NOI18N
        bntSearchPro.setText("Search");
        bntSearchPro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntSearchProActionPerformed(evt);
            }
        });
        add(bntSearchPro, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 370, -1, -1));

        CbbCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Selected--" }));
        CbbCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CbbCategoryActionPerformed(evt);
            }
        });
        add(CbbCategory, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 370, 110, -1));

        bntCreateProduct.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        bntCreateProduct.setText("Create Product");
        bntCreateProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCreateProductActionPerformed(evt);
            }
        });
        add(bntCreateProduct, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 410, 130, -1));

        bntCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/posts.gif"))); // NOI18N
        bntCategory.setText("Category");
        bntCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCategoryActionPerformed(evt);
            }
        });
        add(bntCategory, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 410, -1, -1));

        bntDeleteProduct.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/delete.png"))); // NOI18N
        bntDeleteProduct.setText("Delete Product");
        bntDeleteProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntDeleteProductActionPerformed(evt);
            }
        });
        add(bntDeleteProduct, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 410, -1, -1));

        bntEditProduct.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/edit.png"))); // NOI18N
        bntEditProduct.setText("Edit Product");
        bntEditProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntEditProductActionPerformed(evt);
            }
        });
        add(bntEditProduct, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 410, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void bntSearchProActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntSearchProActionPerformed
        try {
            SelectProduct();
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bntSearchProActionPerformed

    private void CbbCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CbbCategoryActionPerformed
        try {
            product_BLL pb = new product_BLL();
            Vector vt = pb.selectProductByCategory(CbbCategory.getSelectedItem().toString());
            Vector vtCol = new Vector();
            vtCol.add("productid");
            vtCol.add("typeinfo");
            vtCol.add("productname");
            vtCol.add("unitprice");
            vtCol.add("quantity");
            DefaultTableModel dtm = new DefaultTableModel(vt, vtCol) {

                @Override
                public boolean isCellEditable(int rowIndex, int mColIndex) {
                    return false;
                }
            };

            tblProduct.setModel(dtm);
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_CbbCategoryActionPerformed

    private void bntCreateProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCreateProductActionPerformed
        try {
            CreateProduct np = new CreateProduct(null, true);
            np.show();
            SelectProduct();
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bntCreateProductActionPerformed

    private void bntEditProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntEditProductActionPerformed
        int currentline = tblProduct.getSelectedRow();
        if (currentline < 0) {
            JOptionPane.showMessageDialog(this, "Select a line");
        } else {
            try {
                variable.Productid = Integer.parseInt(tblProduct.getValueAt(currentline, 0).toString());
                variable.ProTypeinfo = tblProduct.getValueAt(currentline, 1).toString();
                variable.Productname = tblProduct.getValueAt(currentline, 2).toString();
                variable.ProQuantity = Integer.parseInt(tblProduct.getValueAt(currentline, 4).toString());
                variable.ProUnitprice = Float.parseFloat(tblProduct.getValueAt(currentline, 3).toString());
                EditProduct ep = new EditProduct(null, true);
                ep.setVisible(true);
                SelectProduct();
            } catch (IOException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_bntEditProductActionPerformed

    private void bntDeleteProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntDeleteProductActionPerformed
        int reply = JOptionPane.showConfirmDialog(this, "Sure you delete", null, JOptionPane.OK_CANCEL_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            int currentline = tblProduct.getSelectedRow();
            try {
                if (currentline < 0) {
                    JOptionPane.showMessageDialog(this, "Select a line");
                } else {
                    int a = Integer.parseInt(String.valueOf(tblProduct.getValueAt(currentline, 0)));
                    orderline_BLL odlb = new orderline_BLL();
                    Vector vt = odlb.selectByProductid(a);
                    if (vt.isEmpty()) {
                        product_BLL pdb = new product_BLL();
                        pdb.deleteproduct(a);
                        SelectProduct();
                        JOptionPane.showMessageDialog(this, "Successful Delete");
                    } else {
                        JOptionPane.showMessageDialog(this, "Cannot delete as products still recorded in bills");
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_bntDeleteProductActionPerformed

    private void bntCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCategoryActionPerformed
        try {
            Category qc = new Category(null, true);
            qc.show();
            SelectProduct();
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bntCategoryActionPerformed

    private void txtProductidKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtProductidKeyTyped
    int key = evt.getKeyChar();
        String st = txtProductid.getText();
        String stTest = "0123456789";
        if (key != KeyEvent.VK_BACK_SPACE
                && key != KeyEvent.VK_DELETE
                && key != KeyEvent.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
            }
            if (flag > 0) {
                evt.consume();
            }
        }        // TODO add your handling code here:
    }//GEN-LAST:event_txtProductidKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox CbbCategory;
    private javax.swing.JButton bntCategory;
    private javax.swing.JButton bntCreateProduct;
    private javax.swing.JButton bntDeleteProduct;
    private javax.swing.JButton bntEditProduct;
    private javax.swing.JButton bntSearchPro;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTextField txtProductName;
    private javax.swing.JTextField txtProductid;
    // End of variables declaration//GEN-END:variables
}
